package com.pdg.islandinterloper.clientstatehandlers;

import android.app.AlertDialog;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.pdg.islandinterloper.MainActivity;
import com.pdg.islandinterloper.R;
import com.pdg.islandinterloper.baseclasses.ClientStateHandlerBase;
import com.pdg.islandinterloper.database.objects.Profile;
import com.pdg.islandinterloper.enums.ClientState;

public class NewProfileClientStateHandler extends ClientStateHandlerBase<MainActivity>{

	public NewProfileClientStateHandler(MainActivity theActivity) {
		super(theActivity);
	}

	@Override
	public void handleHide() {
	}

	@Override
	public void handleButtonClick(View theView) {
		switch(theView.getId()){
		case R.id.button_back:
			getActivity().setState(ClientState.PLAY);
			break;
		case R.id.button_create_profile:
			handleCreateProfileButton();
			getActivity().setState(ClientState.UNINITIALIZED_PROFILE);
			break;
		}
	}

	private void handleCreateProfileButton() {
		EditText theEditText = (EditText)getActivity().findViewById(R.id.edit_profile_name);
		String theProfileName =  theEditText.getText().toString();
		if(theProfileName.length()==0){
			showZeroLengthProfileNameAlert();
		}else if(getActivity().getDatabase().getProfileTableProvider().hasProfileByName(theProfileName)){
			showDuplicateProfileNameAlert();
		}else{
			Profile theProfile = new Profile(theProfileName);
			theProfile = getActivity().getDatabase().getProfileTableProvider().createProfile(theProfile);
			getActivity().setProfileId(theProfile.getId());
		}
	}

	private void showDuplicateProfileNameAlert() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.alert_title_error)
		.setMessage(R.string.alert_message_profile_exists)
		.setNeutralButton(R.string.button_ok,null);
		builder.show();
	}

	private void showZeroLengthProfileNameAlert() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.alert_title_error)
		.setMessage(R.string.alert_message_zero_length_profile_name)
		.setNeutralButton(R.string.button_ok,null);
		builder.show();
	}

	@Override
	public boolean handleMenu(MenuItem theItem) {
		return false;
	}

	@Override
	public void handleShow() {
	}

}
